package com.info33.platform.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.info33.platform.common.MQservice.IMessageService;
import com.info33.platform.common.exception.BaseException;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.util.DateUtil;
import com.info33.platform.common.util.MQConstantUtil;
import com.info33.platform.common.util.MQMessageUtil;
import com.info33.platform.system.dao.SysLogDao;
import com.info33.platform.system.entity.SysLog;
import com.info33.platform.system.service.SysLogService;
import com.info33.platform.system.vo.SysUserVO;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.unit.DataUnit;

import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.Date;

/**
 * @author admin
 */
@Service
@Slf4j
public class SysLogServiceImpl extends ServiceImpl<SysLogDao, SysLog> implements SysLogService {

    @Autowired
    private SysLogDao sysLogDao;

    @Autowired
    private IMessageService messageService;

    @Override
    public Result<Object> pcClear(SysUserVO currentUser) {
        return sysLogDao.pcClear(currentUser) > 0 ? Result.ok() : Result.error();
    }

    @Override
    public Result<?> loginSessionList() throws BaseException {
        //获得订单关闭时间（2分钟）
        int closeSecond = 2 * 60;

        //发送延时消息
        MQMessageUtil mqMessageUtil = new MQMessageUtil(MQConstantUtil.QUEUE_HELLO_NAME, 123456 + "_" + 1);
        mqMessageUtil.setType(2);
        mqMessageUtil.setSeconds(closeSecond);
        messageService.send(mqMessageUtil);

        Subject subject = SecurityUtils.getSubject();
        String s = subject.getSession().getId().toString();
        System.out.println(s);
        return Result.ok(s);
    }

    @RabbitHandler
    @RabbitListener(queues = MQConstantUtil.QUEUE_HELLO_NAME)
    private void listenToSpecifiedQueue(String content, Message message, Channel channel) throws IOException {
        System.err.println(content);
        System.err.println(message);
        System.err.println(channel);
        // 根据下划线分割判断订单处于哪种状态
        String[] result = content.split("_");
        String dateTime = DateUtil.getDateTime(new Date());
        System.out.println("当前时间:" + dateTime);
        System.out.println("获取内容:" + content);

        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            if (Integer.parseInt(result[1]) == 1){
                log.error("手动ack");
                channel.basicAck(deliveryTag, false);
            }else {
                log.info("消息即将再次返回队列处理");
                // requeue为是否重新回到队列，true重新入队
                channel.basicNack(deliveryTag, false, true);
            }
        } catch (IOException e) {
            e.printStackTrace();
//            if (message.getMessageProperties().getRedelivered()) {
//                log.info("消息已重复处理失败,拒绝再次接收");
//                // 拒绝消息，requeue=false 表示不再重新入队，如果配置了死信队列则进入死信队列
//                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
//            }
        }
    }
}